<template>
  <div class="settings-container">
    <div class="container">
      <div class="label">Global Switch</div>
      <SwitchTemp :enabled.sync="enabled" @change="handleChange" />
    </div>
    <!-- 类型 -->
    <div class="container">
      <div class="label">Type</div>
      <!-- 按钮组 -->
      <div class="checkbox-button-container">
        <div
          :class="{ focus: ajaxType.fetch }"
          @click="handleChangeType('fetch')"
        >
          Fetch
        </div>
        <div :class="{ focus: ajaxType.xhr }" @click="handleChangeType('xhr')">
          XHR
        </div>
      </div>
    </div>
    <!-- 协议 -->
    <div class="container">
      <div class="label">Protocol</div>
      <!-- 按钮组 -->
      <div class="checkbox-button-container">
        <div :class="{ focus: protocol.get }" @click="handleChangePro('get')">
          GET
        </div>
        <div :class="{ focus: protocol.post }" @click="handleChangePro('post')">
          POST
        </div>
        <div :class="{ focus: protocol.put }" @click="handleChangePro('put')">
          PUT
        </div>
        <div
          :class="{ focus: protocol.delete }"
          @click="handleChangePro('delete')"
        >
          DELETE
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import SwitchTemp from "@/components/Switch.vue";
import {
  getAjaxType,
  getGlobalSwitchOn,
  getProtocol,
  setAjaxType,
  setGlobalSwitchOn,
  setProtocol,
} from "@/common/store";
import { initStore } from "@/common";
export default {
  components: {
    SwitchTemp,
  },
  data() {
    return {
      enabled: false,
      ajaxType: {
        xhr: false,
        fetch: false,
      },
      protocol: {
        get: false,
        post: false,
        put: false,
        delete: false,
      },
    };
  },
  methods: {
    async initData() {
      this.enabled = await getGlobalSwitchOn();
      this.ajaxType = await getAjaxType();
      this.protocol = await getProtocol();
    },
    handleChange(bool) {
      setGlobalSwitchOn(bool);
    },
    handleChangeType(type) {
      this.ajaxType[type] = !this.ajaxType[type];
      setAjaxType(this.ajaxType);
    },
    handleChangePro(pro) {
      this.protocol[pro] = !this.protocol[pro];
      setProtocol(this.protocol);
    },
  },
  created() {
    this.initData();
    // 添加初始化Storage数据
    initStore("init");
  },
};
</script>

<style lang="scss" scoped>
@import "~@/styles/index.scss";

.settings-container {
  @include set_w_h;
  padding: 20px;
  .container {
    margin-bottom: 25px;
    .label {
      opacity: 0.7;
      transition: opacity 0.3s;
      font-size: 14px;
      margin-bottom: 8px;
    }
    .checkbox-button-container {
      @include flex_item_center;
      & > div {
        cursor: pointer;
        margin-right: 1px;
        background: #e0f8ed;
        font-size: 13px;
        color: #2c3e50;
        &:first-child {
          border-radius: 3px 0 0 3px;
        }
        &:last-child {
          border-radius: 0 3px 3px 0;
        }
        &:hover {
          background-color: #defbee;
        }
        &:active {
          background: #c0f3db;
        }
        @include set_w_h(100px, 30px);
        @include flex_center;
      }
      .focus {
        background: #2c3e50;
        color: $FontFFFColor;
        &:hover {
          background: #4e6e8e;
        }
        &:active {
          background: #2c3e50;
        }
      }
    }
  }
}
</style>
